<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>用户编辑</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css" />
    <script src="/static/js/common/common.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" method="post">
    <!-- 用户 编辑框START-->
    <div id="editform" class="form">
        <table class="form-table" border="0" cellspacing="10"
               style="width: 100%; table-layout: fixed;">
            <input id="orgid" name="orgName" class="mini-hidden" style="width: 100%;" readonly />
            <input name="departName" class="mini-hidden" style="width: 100%;" readonly />
            <input name="roleName" class="mini-hidden" style="width: 100%;" readonly />
            <input name="positionName" class="mini-hidden" style="width: 100%;" readonly />
            <tr>
                <td style="width: 18px"><font color="#dc143c">*</font>账号:</td>
                <td style="width: 75px">
                    <input name="account" id="id" class="mini-textbox" style="width: 100%;" required="true" />
                </td>
                <td style="width: 18px"><font color="#dc143c">*</font>用户名:</td>
                <td style="width: 75px">
                    <input name="username" class="mini-textbox" style="width: 100%;" required="true" />
                </td>
            </tr>
            <tr>
                <td style="width: 18px">上级单位:</td>
                <td style="width: 75px">
                    <input name="orgid" class="mini-combobox" style="width:100%;" required="true" enabled="false"
                           textField="name" valueField="id" />
                </td>
                <td style="width: 75px;"><font color="#dc143c">*</font>机构:</td>
                <td>
                    <input id="editDepartmentName" name="departid" class="mini-combobox" style="width:100%;"
                           required="true"
                           textField="name" valueField="id" emptyText="请选择..." />
                </td>
            </tr>
            <tr>
                <td style="width: 18px"><font color="#dc143c">*</font>角色:</td>
                <td style="width: 75px">
                    <input id="editRoleName" name="roleid" class="mini-combobox" style="width:100%;" required="true" popupMaxHeight="160px" alwaysView
                           textField="name" valueField="roleid" emptyText="请选择..." />
                </td>
                <td style="width: 18px"><font color="#dc143c">*</font>岗位:</td>
                <td style="width: 75px">
                    <input id="editPositionName" name="position" class="mini-combobox" style="width:100%;" required="true" popupMaxHeight="160px"
                           alwaysView
                           textField="name" valueField="id" emptyText="请选择..." />
                </td>
            </tr>
            <tr>
                <td style="width: 18px">固话:</td>
                <td style="width: 75px">
                    <input name="telphone" id="telphone" class="mini-textbox" style="width: 100%;" onblur="checkTel(telphone)" emptyText="请输入固定电话" />
                </td>
                <td style="width: 18px">&nbsp;邮箱:</td>
                <td style="width: 75px">
                    <input name="email" id="email" class="mini-textbox" style="width: 100%;" onblur="checkEmail(email)" />
                </td>
            </tr>
            <tr>
                <#if sso>
                    <td style="width: 40px"><font color="#dc143c">*</font>手机号码:</td>
                    <td style="width: 140px">
                        <input name="phone" id="phone" class="mini-textbox" style="width: 100%;" onblur="checkPhone(phone)" emptyText="单点登录手机号码必填" required/>
                    </td>
                <#else >
                    <td style="width: 18px">手机号码:</td>
                    <td style="width: 150px">
                        <input name="phone" id="phone" class="mini-textbox" style="width: 100%;" onblur="checkPhone(phone)" emptyText="请输入手机号码" />
                    </td>
                </#if>
            </tr>
            <tr>
                <td>
                    <input id="sso" name="sso" type="hidden" value="${sso}">
                </td>
            </tr>
        </table>
    </div>
    <!-- 用户 编辑框END-->
    <div style="padding:2px;margin: 22px;text-align: center">
        <a class="mini-button" iconCls="fa-save" plain="true" onclick="saveData()">保存</a>
        <a class="mini-button" iconCls="fa-close" plain="true" onclick="onCancel()">取消</a>
    </div>
</form>

<script type="text/javascript">
    mini.parse();
    var form = new mini.Form("form1");

    //标准方法接口定义
    function SetData(data) {
        var o = mini.decode(data);
        var flag = ${flag!};//判断是新增还是修改操作的标识 1标识新增 2标识回显
        if (flag == 1) {//新增回显
            var orgid = o.id;//本级机构code编码
            var pid = o.pid;//上级机构code编码
            var org = mini.getbyName("orgid");
            org.setValue(o.pid);
            org.setText(o.pname);
            var depart = mini.getbyName("departid");
            depart.setValue(o.id);
            depart.setText(o.name);
            mini.get("editDepartmentName").disable(false);
            roleNameEdit(orgid);
            positionNameEdit(orgid);
        }
        if (flag == 2) {//修改回显
            var orgid = o.orgid;//上级机构code编码
            var departid = o.departid;//本级code编码
            mini.get("editDepartmentName").disable(false);
            roleNameEdit(departid);
            positionNameEdit(departid);
            if (o.action != "add") {
                form.setData(o);
                form.setChanged(false);
                mini.getbyName("account").disable();
                var userId = mini.get("id");
                UserInfoEdit(userId.getValue());
            }
        }
    }

    function saveData() {
        //如果是新增需要判断数据库中是否存在该岗位编号
        var mark = false;
        var telphone = mini.get("telphone").getValue();
        var phone = mini.get("phone").getValue();
        var newPhone = checkTelAndPhone(telphone,phone);
        var flag = newPhone['flag'];
        var kflag = newPhone['kflag'];
        //单点登录省份手机号为必填
        var sso = $("#sso").val();  //单点登录标识
        var phoneFlag = false;  //手机号存在标识
        var account = mini.get("id").getValue();    //当前账户
        if (sso){
            if (phone == '') {
                mini.alert("单点登录省份手机号必填");
                return;
            }
            $.ajax({
                url: "/basic/user/phone",
                data:{
                    phoneNum : phone,
                    account : account
                },
                cache: false,
                async: false,
                type: 'post',
                success: function (success) {
                    if (success.code == 1) {
                        phoneFlag = true;
                    } else {
                    }
                }
            });
            if (!phoneFlag) {
                mini.alert("该手机号码已被注册，请重新输入");
                return false;
            }
        }
        if('-1'===kflag){
            mini.alert("请至少输入一个联系方式");
            return;
        }
        if (${flag!} == 1) {
            $.ajax({
                url: "/basic/user/account/" + mini.get("id").getValue(),
                cache: false,
                async: false,
                success: function (success) {
                    if (success.code == 1) {
                        mark = true;
                    } else {

                    }
                }
            });
        }
        if (mark == true) {
            mini.alert("该用户已存在");
        } else {
            var departid = mini.getbyName("departid");
            form.validate();
            if (form.isValid() == false) {
                return;
            }
            if (departid.getValue() === '') {
                mini.alert("参数错误,机构不能为空");
                return;
            }
            if (checkPhone("phone") == false) {
                return;
            }
            if (checkTel("telphone") == false) {
                return;
            }
            if (checkEmail("email") == false) {
                return;
            }
            var data = form.getData();
            var json = mini.encode(data);
            commonSaveData("/basic/user/save", {data: json}, callback);

            // 保存成功的回调函数
            function callback() {
                mini.alert("保存成功", "成功", window.CloseOwnerWindow);
            }
        }
    }

    function roleNameEdit(orgId) {
        commonComboBoxSearch("/basic/user/getUserRoleList/" + orgId, {data: -1}, callback);

        function callback(success) {
            var editRoleName = mini.get("editRoleName");
            editRoleName.load(mini.decode(success));
        }
    }

    function positionNameEdit(orgid) {
        commonComboBoxSearch("/basic/userPositionNameEdit/" + orgid, {data: -1}, callback);

        function callback(success) {
            var editPositionName = mini.get("editPositionName");
            editPositionName.load(mini.decode(success));
        }
    }

    function UserInfoEdit(userId) {
        commonComboBoxSearch("/basic/user/account/" + userId, {data: -1}, callback);

        function callback(success) {
            var data = JSON.parse(success.data);
            mini.get("phone").setValue(data.phone);
            mini.get("telphone").setValue(data.telphone);
            mini.get("email").setValue(data.email);
            mini.getbyName("orgid").setData([{id: data.orgid, name: data.orgName}]);
            mini.getbyName("departid").setData([{id: data.departid, name: data.departName}]);
        }
    }

    //手机号码的验证
    function checkPhone(phone) {
        var phone = mini.get("phone").getValue();
        if (phone == '') {
            return true;
        }
        if (!(/^1[3456789]\d{9}$/.test(phone))) {
            mini.alert("手机号码有误，请重填");
            return false;
        }
    }
    //固定电话的验证
    function checkTel(telphone) {
        if (mini.get(telphone).getValue() == ''){
            return true;
        }
        var tel = mini.get(telphone).getValue();
        if (!/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(tel)) {
            mini.alert('固定电话有误，请重填');
            return false;
        }
    }

    //邮箱的验证
    function checkEmail(email) {
        if (mini.get(email).getValue() == '') {
            return true;
        }
        var email = mini.get(email).getValue();
        if (!(/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/.test(email))) {
            mini.alert("邮箱有误，请重填");
            return false;
        }
    }
</script>
</body>
</html>